//
//  JoAlert.swift
//  soapp_Example
//
//  Created by zhu xietong on 2017/11/8.
//  Copyright © 2017年 CocoaPods. All rights reserved.
//

import Foundation
import Eelay


enum JoAlertAnimate {
    case `default`
}


public protocol EeAlert {
    func show()
    func dismiss()
    var contentView:UIView{get set}
}


public class InfoAlert: EeAlertView {
    public override var intrinsicContentSize: CGSize{
        return [300,200]
    }
}

open class EeAlertView:JoView,EeAlert{
    
    public var enableTouchDismiss = true
    
    public var centerOffset:CGPoint = CGPoint.zero
    open override func addLayoutRules() {
        self.backgroundColor = .white
    }
    
    @objc open func close()
    {
        self.touchBK?.dismiss()
    }
    
    open func show() {
        
//        self.alpha = 0
//        UIView.beginAnimations(nil, context: nil)
//        UIView.setAnimationDuration(0.29)
//        UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: 7)!)
//        self.alpha = 1
//        UIView.commitAnimations()
        
        weak var wself = self
        
        let previousTransform = self.contentView.transform
        self.contentView.layer.transform = CATransform3DMakeScale(0.8, 0.8, 0.0);
        self.alpha = 0
        UIView.animate(withDuration: 0.12, animations: { () -> Void in
            wself?.alpha = 1
            wself?.contentView.layer.transform = CATransform3DMakeScale(1.005, 1.005, 0.0);
        }, completion: { (Bool) -> Void in
            UIView.animate(withDuration: 0.1, animations: { () -> Void in
                wself?.contentView.layer.transform = CATransform3DMakeScale(0.99, 0.99, 0.0);
            }, completion: { (Bool) -> Void in
                UIView.animate(withDuration: 0.1, animations: { () -> Void in
                    wself?.contentView.layer.transform = CATransform3DMakeScale(1.0, 1.0, 0.0);
//                    if self.animatedView != nil {
//                        self.animatedView!.animate()
//                    }
                    
                }, completion: { (Bool) -> Void in
                    
                    wself?.contentView.transform = previousTransform
                })
            })
        })
        
        let touchBack = EeTouchBack()
        touchBack.enableTouchDismiss = self.enableTouchDismiss
        touchBack.alert = self
        touchBack.show()
        self.touchBK = touchBack
    }
    
    open var maskLayerColor:UIColor{
        get{
            return .color(light: "#00000054", dark: "#3335")
        }
    }
    
    open func dismiss() {
//        self.alpha = 1
//        UIView.beginAnimations(nil, context: nil)
//        UIView.setAnimationDuration(0.29)
//        UIView.setAnimationCurve(UIView.AnimationCurve(rawValue: 7)!)
//        self.alpha = 0
//        UIView.commitAnimations()
        
        self.touchBK?.dismiss()
    }
    
    open var contentView: UIView{
        get{
            return self
        }
        set{}
    }
    
    public weak var touchBK: EeTouchBack? = nil
}


open class EeTouchBack:UIControl
{
    
    public var enableTouchDismiss = true

    
    public var __constrains = [NSLayoutConstraint]()
    
    open var rootBack:UIView?{
        get{
            var back:UIView? = UIApplication.shared.keyWindow
            if back == nil{
                back = UIApplication.shared.keyWindow?.rootViewController?.view
            }
            return back
        }
    }
    
    public weak var alert:EeAlertView? = nil
    {
        didSet{
            if let view = alert?.contentView
            {
                let offset = alert?.centerOffset ?? .zero
                self.eelay = [
                    [view,[ee.X.Y,[offset.x,offset.y]]]
                ]
            }
        }
    }
    
    public init() {
        super.init(frame: CGRect.zero)
        self.addTarget(nil, action: #selector(__close), for: .touchUpInside)
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        self.addTarget(nil, action: #selector(__close), for: .touchUpInside)
    }
    
    @objc public func __close() {
        endEditing(true)
        if !enableTouchDismiss{return}
        self.dismiss()
    }
    

    
    @objc open func dismiss() {
    
        if let backV = self.rootBack
        {
//            self.alert?.dismiss()
            var backColor:UIColor = .color(light: "#0008", dark: "1118")
            if let _alert = self.alert{
                _alert.alpha = 1
                UIView.beginAnimations(nil, context: nil)
                UIView.setAnimationDuration(0.29)
                UIView.setAnimationCurve(UIView.AnimationCurve(rawValue: 7)!)
                self.alpha = 0
                UIView.commitAnimations()
                backColor = _alert.maskLayerColor

            }
            
            weak var wself = self
            backgroundColor = backColor
            UIView.animate(withDuration: 0.3, animations: {
                wself?.backgroundColor = UIColor(shex:"0000")
            }, completion: { (finish) in
                if finish
                {
                    wself?.backgroundColor = UIColor(shex:"0000")
                    wself?.removeFromSuperview()
                    if let cs = wself?.__constrains
                    {
                        backV.removeConstraints(cs)
                    }
                }
            })
        }
    }
    
    open func show() {
        if let backV = self.rootBack
        {
            let lays = [
                [self,[ee.T.L.B.R]],
            ]
            self.__constrains = backV.setEeLays(lays: lays).1
            
            let backColor = self.alert?.maskLayerColor
//            self.alert?.show()
            weak var wself = self
            self.backgroundColor = UIColor(shex:"0000")
            UIView.animate(withDuration: 0.25, animations: {
                wself?.backgroundColor = backColor
            }, completion: { (finish) in
                if finish
                {
                    wself?.backgroundColor = backColor
                }
            })
        }
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
